import CryptoJS from "crypto-js";
import {ReturnResult} from "@/utils/ReturnResult";

const key = CryptoJS.enc.Utf8.parse("smartschool320..");

/**
 * 验证邮箱格式是否有效
 * @param email - 需要验证的邮箱地址
 * @returns 如果邮箱格式有效，返回 true；否则返回 false
 */
export const validateEmail = (email: string): boolean => {
    return email.includes('@');
};

export function encrypt(str: string){
    return CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(str), key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    }).toString()
}

export function decrypt(r: ReturnResult){
    const resultData = CryptoJS.AES.decrypt(r.data, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    }).toString()

    let hex = resultData.toString();

    let byteArray = [];
    for (let i = 0; i < hex.length; i += 2) {
        byteArray.push(parseInt(hex.substr(i, 2), 16));
    }

    let decoder = new TextDecoder('utf-8');
    return decoder.decode(new Uint8Array(byteArray))
}
